package com.sevlet;

import com.Bean.RoomAndRoomType;
import com.Bean.RoomAndRoomType1;
import com.Bean.RoomId;
import com.dao.impl.RoomIdDaoimpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.util.JsonUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@WebServlet("/RoomSevlet")
public class RoomSevlet extends BaseSevlet{
//public class RoomSevlet extends HttpServlet {

    public void selectRoomWhereType(HttpServletRequest req, HttpServletResponse resp){

        String typename = req.getParameter("typename");
        RoomIdDaoimpl rdi=new RoomIdDaoimpl();
        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        PrintWriter out = null;
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        resp.setContentType("text/html;charset=utf-8");
        try {

            //List<RoomId> roomIds = rdi.queryRoomWereTypename(typename);
            List<RoomId> roomIds = rdi.queryRoomWereTypename(typename);
            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");
            jsonUtil.setData(roomIds);
        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误!");
        }finally {


                out.print(gson.toJson(jsonUtil));

        }
    }

    public void selectRoomType(HttpServletRequest req, HttpServletResponse resp){

        RoomIdDaoimpl rdi=new RoomIdDaoimpl();
        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        PrintWriter out = null;
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        resp.setContentType("text/html;charset=utf-8");
        try {

            List<com.Bean.Room> rooms = rdi.queryRoomType();
            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");
            jsonUtil.setData(rooms);
        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误!");
        }finally {


            out.print(gson.toJson(jsonUtil));

        }
    }
    //查所有的房间类型和房间
    public void selectRoomAndType(HttpServletRequest req, HttpServletResponse resp){
        //String date = req.getParameter("date");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse =null;String format =null;
//        try {
//            parse = simpleDateFormat.parse(date);
//            format = simpleDateFormat.format(parse);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        RoomIdDaoimpl rdi=new RoomIdDaoimpl();
        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        gsonBuilder.setDateFormat("yyyy-MM-dd");
        PrintWriter out = null;
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        resp.setContentType("text/html;charset=utf-8");
        try {

            List<RoomAndRoomType> rooms = rdi.queryRoomandType();
            System.out.println("rooms"+rooms);
            ArrayList<RoomAndRoomType1> sortroom = sortroom(rooms);
            System.out.println("sortroom:"+sortroom);
            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");
            jsonUtil.setData(sortroom);
        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误!");
        }finally {


            out.print(gson.toJson(jsonUtil));

        }
    }
    //查所有的房间类型和房间
    public void selectRoomAndType2(HttpServletRequest req, HttpServletResponse resp){
        //String date = req.getParameter("date");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse =null;String format =null;

        RoomIdDaoimpl rdi=new RoomIdDaoimpl();
        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        gsonBuilder.setDateFormat("yyyy-MM-dd");
        PrintWriter out = null;
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        resp.setContentType("text/html;charset=utf-8");
        try {

            List<RoomAndRoomType> rooms = rdi.queryRoomandType();

            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");
            jsonUtil.setData(rooms);
        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误!");
        }finally {


            out.print(gson.toJson(jsonUtil));

        }
    }
    public ArrayList<RoomAndRoomType1> sortroom(List<RoomAndRoomType> rooms){
        System.out.println("111");

        ArrayList<RoomAndRoomType1> roomAndRoomType1s = new ArrayList<>();//房间and类型
        String Rtid="";
        RoomId roomId=null;
        int flag=0;
        ArrayList<RoomId> roomIds = new ArrayList<>();//房间
        RoomAndRoomType room1=null;RoomAndRoomType roomfro=null;
        for (RoomAndRoomType room : rooms) {
            flag=0;
            room1=room;
            if (Rtid==""||Rtid.equals(room.getRtid())){

                roomId = new RoomId();
                roomId.setRid(room.getRid());
                roomId.setRstatus(room.getRstatus());
                roomId.setTimetype(room.getTimetype());
                roomId.setRtid(room.getRtid());
                roomIds.add(roomId);
                Rtid=room.getRtid();
                roomfro=room;
            }else if (!Rtid.equals(room.getRtid())){
                flag+=1;

                RoomAndRoomType1 roomAndRoomType1 = new RoomAndRoomType1();
                roomAndRoomType1.setRoomids(roomIds);
                roomAndRoomType1.setRname(roomfro.getRname());
                roomAndRoomType1.setRnumber(roomfro.getRnumber());
                roomAndRoomType1.setWeprice(roomfro.getWeprice());
                roomAndRoomType1.setWkprice(roomfro.getWkprice());
                roomAndRoomType1s.add(roomAndRoomType1);

                roomIds = new ArrayList<>();//房间
                roomId = new RoomId();
                roomId.setRid(room.getRid());
                roomId.setRstatus(room.getRstatus());
                roomId.setTimetype(room.getTimetype());
                roomId.setRtid(room.getRtid());
                roomIds.add(roomId);
                Rtid=room.getRtid();

            }


        }
//            if (flag==1){
//                RoomAndRoomType1 roomAndRoomType1 = new RoomAndRoomType1();
//                roomAndRoomType1.setRoomids(roomIds);
//                roomAndRoomType1.setRname(room1.getRname());
//                roomAndRoomType1.setRnumber(room1.getRnumber());
//                roomAndRoomType1.setWeprice(room1.getWeprice());
//                roomAndRoomType1.setWkprice(room1.getWkprice());
//                roomAndRoomType1s.add(roomAndRoomType1);
//            }
            RoomAndRoomType1 roomAndRoomType1 = new RoomAndRoomType1();
            roomAndRoomType1.setRoomids(roomIds);
            roomAndRoomType1.setRname(room1.getRname());
            roomAndRoomType1.setRnumber(room1.getRnumber());
            roomAndRoomType1.setWeprice(room1.getWeprice());
            roomAndRoomType1.setWkprice(room1.getWkprice());
            roomAndRoomType1s.add(roomAndRoomType1);

        System.out.println("roomAndRoomType1s"+roomAndRoomType1s);
        return roomAndRoomType1s;
    }
}
